package ch.ethz.fcl.metrobuzz.controller.gui.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;
import ch.ethz.fcl.metrobuzz.MBContainer;

public class ControlPanel extends JPanel {
	private static final long serialVersionUID = -8838914717136547691L;

	private JButton exit;
	
	private MBContainer container;

	public ControlPanel(MBContainer container) {
		this.container = container;
		
		setBackground(Color.lightGray);
		setLayout(new MigLayout());

		ButtonListener bl = new ButtonListener();

		JPanel direction = new JPanel();
		direction.setBackground(Color.LIGHT_GRAY);
		direction.setBorder(BorderFactory.createTitledBorder("Direction"));
		direction.setLayout(new GridLayout(3, 3));

		JPanel[][] temp_panels = new JPanel[3][3];
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++) {
				temp_panels[i][j] = new JPanel();
				temp_panels[i][j].setBackground(Color.LIGHT_GRAY);
				direction.add(temp_panels[i][j]);
			}

		add(new TimePanel(this.container), "span, grow");
		add(new ViewerControlPanel(this.container), "span, grow");

		exit = new JButton("Exit");
		exit.addChangeListener(bl);
		exit.setPreferredSize(new Dimension(30, 30));
		add(exit);
	}

	class ButtonListener implements ChangeListener {
		@Override
		public void stateChanged(ChangeEvent changeEvent) {
			Object source = changeEvent.getSource();
			if (source instanceof AbstractButton) {
				AbstractButton abstractButton = (AbstractButton) source;
				ButtonModel buttonModel = abstractButton.getModel();
				boolean pressed = buttonModel.isPressed();
				if (abstractButton.equals(exit)) {
					if (pressed)
						System.exit(0);
				}
			}
		}
	}
}
